package com.yunji.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunji.news.common.Result;
import com.yunji.news.entity.PaymentRecord;
import com.yunji.news.service.PaymentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 缴费记录控制器
 *
 * @author yunji
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/payment")
@CrossOrigin
public class PaymentRecordController {

    @Autowired
    private PaymentRecordService paymentService;

    /**
     * 分页查询缴费记录
     */
    @GetMapping
    public Result<IPage<PaymentRecord>> getPayments(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) String paymentType,
            @RequestParam(required = false) String paymentStatus,
            @RequestParam(required = false) String keyword) {
        
        IPage<PaymentRecord> result = paymentService.getPaymentPage(
            page, size, schoolId, paymentType, paymentStatus, keyword);
        return Result.success(result);
    }

    /**
     * 获取缴费记录详情
     */
    @GetMapping("/{id}")
    public Result<PaymentRecord> getPayment(@PathVariable Long id) {
        PaymentRecord payment = paymentService.getById(id);
        if (payment == null) {
            return Result.error("缴费记录不存在");
        }
        return Result.success(payment);
    }

    /**
     * 创建缴费记录
     */
    @PostMapping
    public Result<String> createPaymentRecord(@RequestBody PaymentRecord record) {
        try {
            boolean success = paymentService.createPaymentRecord(record);
            if (success) {
                return Result.success("缴费记录创建成功");
            } else {
                return Result.error("缴费记录创建失败");
            }
        } catch (Exception e) {
            return Result.error("缴费记录创建失败：" + e.getMessage());
        }
    }

    /**
     * 确认缴费
     */
    @PutMapping("/{id}/confirm")
    public Result<String> confirmPayment(
            @PathVariable Long id,
            @RequestParam String transactionId,
            @RequestParam(required = false) String receiptUrl) {
        
        boolean success = paymentService.confirmPayment(id, transactionId, receiptUrl);
        if (success) {
            return Result.success("缴费确认成功");
        } else {
            return Result.error("缴费确认失败");
        }
    }

    /**
     * 申请退费
     */
    @PutMapping("/{id}/refund/apply")
    public Result<String> applyRefund(
            @PathVariable Long id,
            @RequestParam BigDecimal refundAmount,
            @RequestParam String reason) {
        
        boolean success = paymentService.applyRefund(id, refundAmount, reason);
        if (success) {
            return Result.success("退费申请提交成功");
        } else {
            return Result.error("退费申请提交失败");
        }
    }

    /**
     * 确认退费
     */
    @PutMapping("/{id}/refund/confirm")
    public Result<String> confirmRefund(@PathVariable Long id) {
        boolean success = paymentService.confirmRefund(id);
        if (success) {
            return Result.success("退费确认成功");
        } else {
            return Result.error("退费确认失败");
        }
    }

    /**
     * 查询学生缴费状态
     */
    @GetMapping("/student/{idCard}")
    public Result<List<PaymentRecord>> getStudentPayments(
            @PathVariable String idCard,
            @RequestParam(required = false) Long schoolId) {
        
        List<PaymentRecord> payments = paymentService.getStudentPayments(idCard, schoolId);
        return Result.success(payments);
    }

    /**
     * 查询未缴费学生名单
     */
    @GetMapping("/unpaid")
    public Result<List<PaymentRecord>> getUnpaidStudents(
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId) {
        
        List<PaymentRecord> unpaidStudents = paymentService.getUnpaidStudents(schoolId, majorId);
        return Result.success(unpaidStudents);
    }
}